package com.bluesky.android.webservice;

import java.io.File;

import org.ksoap2.serialization.SoapObject;

import android.content.ContentValues;
import android.database.Cursor;
import android.os.Environment;
import android.util.Log;

import com.bluesky.android.utils.*;


public class MangaChapter {
	private String link ="";
	private String title="";
	private String date = "";
	private int chapterId = 0;
	private int mangaId = 0;
	private short saveDataOnDisk = 0;
	private int currentImagePosition = 0;
	private DownloadedEnum chapterIsDownloading = DownloadedEnum.NotDownload;
	public MangaChapter(String link, String title, String date){
		this.link = link;
		this.date = date;
		this.title = title;
	}
	public enum DownloadedEnum{
		NotDownload(0) , Downloading(1), Downloaded(2);
		public static DownloadedEnum fromInteger(int x) {
	        switch(x) {
	        case 0:
	            return NotDownload;
	        case 1:
	            return Downloading;
	        case 2:
	        	return Downloaded;
	        default:
	        	return NotDownload;	        	
	        }	        
	    }
		private int value;
		private DownloadedEnum(int value){
			this.value = value;
		}
	}
	public MangaChapter(){}
	public static MangaChapter GetMangaChapterFromSoapObject(SoapObject  soapObject){
		MangaChapter mangChapter = new MangaChapter();
		if(soapObject.getProperty("Link") != null)
		{
			mangChapter.setLink(soapObject.getProperty("Link").toString() );
		}
		if(soapObject.getProperty("Title") != null)
		{
			mangChapter.setTitle(soapObject.getProperty("Title").toString());
		}
		if(soapObject.getProperty("Date") != null)
		{
			mangChapter.setDate(soapObject.getProperty("Date").toString());
		}
		return mangChapter;
	}
	public void setLink(String link) {
		if(link != null){
			this.link = link;
		}
		
	}
	public String getLink() {
		return link;
	}
	public void setTitle(String title) {
		if(title!= null)
			this.title = title;
	}
	public String getTitle() {
		return title;
	}
	public void setDate(String date) {
		if(date!= null)
			this.date = date;
	}
	public String getDate() {
		return date;
	}
	public int getChapterId() {
		return chapterId;
	}
	public void setChapterId(int chapterId) {
		if(chapterId !=  0){
			this.chapterId = chapterId;
		}
	}
	public int getMangaId() {
		return mangaId;
	}
	public void setMangaId(int mangaId) {
		this.mangaId = mangaId;
	}
	public short getSaveDataOnDisk() {
		return saveDataOnDisk;
	}
	public void setSaveDataOnDisk(short saveDataOnDisk) {
		this.saveDataOnDisk = saveDataOnDisk;
	}
	public int getCurrentImagePosition() {
		return currentImagePosition;
	}
	public void setCurrentImagePosition(int currentImagePosition) {
		if(currentImagePosition > 0){
			this.currentImagePosition = currentImagePosition;
		}else{
			this.currentImagePosition = 0;
		}
	}
	
	public DownloadedEnum getChapterIsDownloading() {
		return chapterIsDownloading;
	}
	public void setChapterIsDownloading(DownloadedEnum chapterIsDownload) {
		this.chapterIsDownloading = chapterIsDownload;
	}
	
	public String getChapterDirectoryPath(String rootMangaDirectory){
		String resultValue = "";
		if(rootMangaDirectory == null || rootMangaDirectory == "" || mangaId < 1){
			Log.i(MangaUtility.MANGA_LOG_TAG, "Get root manga directory error.");
			return resultValue;
		}	
		File dir =  new File(rootMangaDirectory, MangaUtility.md5(getLink() ) );
		if(!dir.exists()){
			dir.mkdir();
		}
		resultValue = dir.getPath();		
		return resultValue;
	}
	
	public static MangaChapter cursorToMangaChapter(Cursor cursor) {
		MangaChapter mangaStory = new MangaChapter();
		try{
			mangaStory.setChapterId(cursor.getInt(cursor.getColumnIndex(MangaContants.CHAPTER_ID) ) );
			mangaStory.setDate(cursor.getString(cursor.getColumnIndex(MangaContants.CHAPTER_DATE)));
			mangaStory.setLink(cursor.getString(cursor.getColumnIndex(MangaContants.CHAPTER_LINK) ) );
			mangaStory.setTitle(cursor.getString(cursor.getColumnIndex(MangaContants.CHAPTER_TITLE) ) );
			mangaStory.setMangaId(cursor.getInt(cursor.getColumnIndex(MangaContants.CHAPTER_MANGA_ID_FK) ) );
			mangaStory.setSaveDataOnDisk(cursor.getShort(cursor.getColumnIndex(MangaContants.CHAPTER_SAVE_ON_DISK) ) );
			mangaStory.setCurrentImagePosition(cursor.getInt(cursor.getColumnIndex(MangaContants.CHAPTER_CURRENT_IMAGE_POSITION) ) );
			mangaStory.setChapterIsDownloading( DownloadedEnum.fromInteger(cursor.getInt(cursor.getColumnIndex(MangaContants.CHAPTER_IS_DOWNLOADING) ) ) );
		}catch(Exception ex){
			ex.printStackTrace();
		}
		return mangaStory;
	}
	public static ContentValues mangaChapterToContentValue(MangaChapter mangaStory){
		ContentValues value = new ContentValues();
		value.put(MangaContants.CHAPTER_DATE, mangaStory.getDate() );
		value.put(MangaContants.CHAPTER_LINK, mangaStory.getLink() );
		value.put(MangaContants.CHAPTER_TITLE, mangaStory.getTitle() );
		value.put(MangaContants.CHAPTER_MANGA_ID_FK, mangaStory.getMangaId() );
		value.put(MangaContants.CHAPTER_SAVE_ON_DISK, mangaStory.getSaveDataOnDisk() );
		value.put(MangaContants.CHAPTER_CURRENT_IMAGE_POSITION, mangaStory.getCurrentImagePosition() );
		value.put(MangaContants.CHAPTER_IS_DOWNLOADING,  Integer.parseInt(mangaStory.getChapterIsDownloading().toString() ) );
		return value;
	}
	
}
